package com.menghanjun.mhjresourcesharing.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String projectPath = System.getProperty("user.dir"); // 获取当前项目的路径
        File projectFile = new File(projectPath); // 构建项目路径的 File 对象
        String projectDrive = projectFile.getParentFile().getAbsolutePath(); // 获取项目所在盘符的绝对路径
        //静态资源访问映射  映射路径 -> 本地资源路径
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:" + projectDrive + "/resources/");
    }
}
